package com.jk.controller;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jk.bean.Author;
import com.jk.bean.Content2;
import com.jk.client.EsService;
import com.jk.service.ElasticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 骚骚的🐏
 * @Author: 杨
 * @Date: 2019/3/8 22:55
 */
@Controller
@RequestMapping("Es")
public class ElashticSerach {
    @Autowired
    ElasticService elasticService;
   @Autowired
    EsService esService;
   @ResponseBody
   @RequestMapping("queryEs")
   public List queryEs(Content2 content2, HttpServletRequest request,HttpServletResponse response ) {
       Cookie[] cookies = request.getCookies();
       String cookievalue="";
       boolean aa=false;
       if(cookies !=null){
           for (Cookie cookie : cookies) {
               if (cookie.getName().equals("searchword")) {
                   cookievalue=cookie.getValue();
                   aa=true;
                   System.out.println(cookievalue);
               }
           }
       }

      //存储cookie
       if(!cookievalue.contains(content2.getText())){
           if(aa){
               Cookie searchword = new Cookie("searchword",content2.getText()+"#"+cookievalue);
               //勾选  记住
               //设置过期时间  保存七天
               searchword.setMaxAge(604800);
               //当前应用任何目录下都能访问cookie
               searchword.setPath("/");
               response.addCookie(searchword);
               //此时cookie还在服务器上  要把cookie发送到浏览器上  通过相应对象
           }else{
               Cookie searchword = new Cookie("searchword",content2.getText());
               //勾选  记住
               //设置过期时间  保存七天
               searchword.setMaxAge(604800);
               //当前应用任何目录下都能访问cookie
               searchword.setPath("/");
               response.addCookie(searchword);
               //此时cookie还在服务器上  要把cookie发送到浏览器上  通过相应对象

           }

       }

       if (content2.getId() == 2) {
           List<Author> list2 = elasticService.queryAutor(content2);
           return list2;
       }
       List<Content2> list = esService.queryEs(content2);
       return list;


   }
  //clearword 清除关键字
    @ResponseBody
    @RequestMapping("clearword")
    public String queryEs( HttpServletRequest request,HttpServletResponse response ) {
        Cookie searchword = new Cookie("searchword","0");
        //勾选  记住
        //设置过期时间  保存七天
        searchword.setMaxAge(0);
        //当前应用任何目录下都能访问cookie
        searchword.setPath("/");
        response.addCookie(searchword);
        //此时cookie还在服务器上  要把cookie发送到浏览器上  通过相应对象
       return "";
    }


    }
